package com.crm.controller;

import com.crm.controller.util.R;
import com.crm.pojo.Goods;
import com.crm.pojo.Order;
import com.crm.service.goodsService;
import com.crm.service.orderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/order")
public class oderController {
    @Autowired
    private orderService service;
    @Autowired
    private goodsService goodsService;


    @GetMapping
    public R queryAll(){
        List<Order> list = service.queryAll();
        return new R(true,list,true? "成功":"失败");
    }
    @PostMapping
    public R save(@RequestBody Order order){
        Goods goods = goodsService.queryOne(order.getGid());
        int price = goods.getPrice();
        int num = order.getNum();
        Date d = new Date();
        Timestamp timestamp = new Timestamp(d.getTime());

        order.setUid(1);
        order.setTotal(price*num);
        order.setDatatime(timestamp);

        boolean flag = service.save(order);
        return new R(flag,null,flag? "提交订单成功":"提交订单失败");
    }
    @DeleteMapping("/{oid}")
    public R removeOne(@PathVariable int oid){
        boolean flag = service.removeOne(oid);
        return new R(flag,null,flag? "删除成功":"删除失败");
    }

}
